package com.cqut.forum.service.imp;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import com.cqut.forum.dao.PictureReplyDao;
import com.cqut.forum.entity.Review;
import com.cqut.forum.service.PictureReplyService;

/**
 * 图片评论实现类
 * @author yangxun
 */
@Controller
public class PictureReplyServiceImp implements PictureReplyService {
	
	@Resource
	private PictureReplyDao pictureReplyDao;
	
	@Override
	public Review save(Review review) {
		review = pictureReplyDao.save(review);
		return review;
	}

	@Override
	public boolean haveReadMyPicture(int userId) {
		boolean result = pictureReplyDao.changeReplyState(userId);
		return result;
	}

	@SuppressWarnings("deprecation")
	@Override
	public List<Map<String, Object>> getPictureReplys(int pictureId) {
		
		List<Integer> pictureIds = new ArrayList<Integer>();
		pictureIds.add(pictureId);
		List<Map<String, Object>> pictureReplys = pictureReplyDao.getPictureReplys(pictureIds);
		List<Map<String, Object>> result = new ArrayList<Map<String,Object>>();
		
		for(Map<String, Object> m:pictureReplys){
			Map<String, Object>	map = new HashMap<String, Object>();
			map.put("image", m.get("user_photo"));
			map.put("name", m.get("user_name"));
			map.put("content", m.get("review_contents"));
			map.put("time", ((Date)m.get("review_time")).toLocaleString());
			map.put("userId", m.get("user_id"));
			map.put("pictureId", m.get("picture_id"));
			map.put("replyId", m.get("review_id"));
			
			result.add(map);
		}
		
		return result;
	}

	@Override
	public boolean deleteById(int replyId) {
		pictureReplyDao.delete(replyId, Review.class);
		return true;
	}
	
}
